package com.example.forum.auto.tests;

import com.example.forum.auto.common.BrowserName;
import com.example.forum.auto.common.Utils;
import lombok.SneakyThrows;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;

/**
 * @author hanzishuai
 * Blog :  https://blog.csdn.net/qrwitu142857
 * @date 2025/5/15 18:07
 * @Description
 */
public class MyArticlePage extends Utils {
    public static final String url = "http://82.157.200.90:58081/index.html";


    public MyArticlePage() {
        super(url);
    }

    public MyArticlePage(BrowserName browserName) {
        super(url, browserName);
    }

    @SneakyThrows
    public void checkPageRight() {
        wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#setting_input_nickname")));
        driver.findElement(By.cssSelector("#setting_input_nickname"));

        driver.findElement(By.cssSelector("#setting_submit_nickname"));
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#setting_submit_nickname")));

        wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#setting_input_email")));
        driver.findElement(By.cssSelector("#setting_input_email"));
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#setting_submit_email")));
        driver.findElement(By.cssSelector("#setting_submit_email"));

        wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#setting_input_phoneNum")));
        driver.findElement(By.cssSelector("#setting_input_phoneNum"));
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#setting_submit_phoneNum")));
        driver.findElement(By.cssSelector("#setting_submit_phoneNum"));

        wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#settings_input_oldPassword")));
        driver.findElement(By.cssSelector("#settings_input_oldPassword"));
        wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#settings_input_newPassword")));
        driver.findElement(By.cssSelector("#settings_input_newPassword"));
        wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#settings_input_passwordRepeat")));
        driver.findElement(By.cssSelector("#settings_input_passwordRepeat"));
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#settings_submit_password")));
        driver.findElement(By.cssSelector("#settings_submit_password"));

        wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#settings_textarea_remark")));
        driver.findElement(By.cssSelector("#settings_textarea_remark"));
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#settings_submit_remark")));
        driver.findElement(By.cssSelector("#settings_submit_remark"));
    }

    private void modify(String nickname, String email, String phoneNum, String oldPassword, String newPassword, String passwordRepeat, String remark) {
        checkPageRight();
        if (nickname != null) {
            driver.findElement(By.cssSelector("#setting_input_nickname")).clear();
            driver.findElement(By.cssSelector("#setting_input_nickname")).sendKeys(nickname);
            driver.findElement(By.cssSelector("#setting_submit_nickname")).click();
        }
        if (email != null) {
            driver.findElement(By.cssSelector("#setting_input_email")).clear();
            driver.findElement(By.cssSelector("#setting_input_email")).sendKeys(email);
            WebElement emailSubmitButton = driver.findElement(By.cssSelector("#setting_submit_email"));
            wait.until(ExpectedConditions.elementToBeClickable(emailSubmitButton));
            sleep();
            emailSubmitButton.click();
        }
        if (phoneNum != null) {
            driver.findElement(By.cssSelector("#setting_input_phoneNum")).clear();
            driver.findElement(By.cssSelector("#setting_input_phoneNum")).sendKeys(phoneNum);
            WebElement element = driver.findElement(By.cssSelector("#setting_submit_phoneNum"));
            wait.until(ExpectedConditions.elementToBeClickable(element));
            element.click();
        }
        if (oldPassword != null) {
            driver.findElement(By.cssSelector("#settings_input_oldPassword")).clear();
            driver.findElement(By.cssSelector("#settings_input_oldPassword")).sendKeys(oldPassword);
        }
        if (newPassword != null) {
            driver.findElement(By.cssSelector("#settings_input_newPassword")).clear();
            driver.findElement(By.cssSelector("#settings_input_newPassword")).sendKeys(newPassword);
        }
        if (passwordRepeat != null) {
            driver.findElement(By.cssSelector("#settings_input_passwordRepeat")).clear();
            driver.findElement(By.cssSelector("#settings_input_passwordRepeat")).sendKeys(passwordRepeat);
        }


        if (remark != null) {
            driver.findElement(By.cssSelector("#settings_textarea_remark")).clear();
            driver.findElement(By.cssSelector("#settings_textarea_remark")).sendKeys(remark);
            driver.findElement(By.cssSelector("#settings_submit_remark")).click();
        }

        if (oldPassword != null || newPassword != null || passwordRepeat != null) {
            driver.findElement(By.cssSelector("#settings_submit_password")).click();
        }

    }

    public void checkModify() {
        modify(null, null, null, null, null, null, null);
        modify("", "", "", "", "", "", "");
        modify("awa1", "abcdef@a.com", null, "123456", null, null, "个人介绍");
        modify("awa1", "abcdef@a.com", "123", "123456", null, null, "个人介绍");
        modify("awa1", "abcdef@a.com", null, "123456", "1234", null, "个人介绍");
        modify("awa1", "abcdef@a.com", null, "123456", "1234", "12", "个人介绍1");
        modify("awa", "abcdef@a.com", null, "123456", "1234", "12", "个人介绍1");
        modify("awa", "123456@qq.com", "123456", "123456", "123456", "123456", "个人介绍");
    }


}
